use core::{config::{CONFIG_PATH, DATA_PATH}, git::REPO};
use std::io;
use command::process_command;
use tokio::runtime::Runtime;

mod core;
mod router;
mod utils;
mod command;

#[tokio::main]
async fn main() {
	// init config args
	CONFIG_PATH.lock().unwrap().push_str("/etc/echest/server.conf");
	DATA_PATH.lock().unwrap().push_str("/usr/share/echest");

	// init
	core::config::init_config();

	let server_rt = Runtime::new().unwrap();
	server_rt.spawn(router::start_server([0, 0, 0, 0], 2333));
	info!(format!("latest version: {}", REPO.lock().unwrap().head().unwrap().target().unwrap()));

	// command line
	loop {
		let mut input = String::new();
		match io::stdin().read_line(&mut input) {
			Ok(_) => {},
			Err(e) => { error!(e); },
		};

		if input.ends_with('\n') { 
			input = input[0..input.len()-1].to_string();
		}

		let mut commands: Vec<String> = input.split(' ').map(|s| s.to_string()).collect();
		commands.retain(|command| command.len() != 0);
		process_command(commands);
	}
}
